/**
 * Created by dell on 16-12-29.
 */

$(document).ready(function() {
    JudgeBrowser.changeTopBarTextSizeInIE();
    if(JudgeLogin.isLogin()) {
        judgeStaffIsNull();
        bindEvent();
    }
});



/* 判断staff是否为空，不为空则跳入staffList界面 */
function judgeStaffIsNull() {
    Api.fetchCompaniesStaff(null, null, null, function (result) {
        if (result.data.content.length != 0) {
            window.location.href = "../../html/home/staffList.html";
        } else {
            setupManagerName();
        }

    }, function (error) {

    })
}

/* 设置管理员名字 */
function setupManagerName() {
    var managerName = window.localStorage.manager;

    Api.fetchCompanyDetailInfo(function (result) {
        $(".managerBaseInfo").text("管理员：" + result.data.companyName);
    }, function (error) {
        alert("获取管理员名字失败");
    })
}


function bindEvent() {
    clickCompanyInfo();
    clickSignOut();
}

/* 点击退出按钮 */
function clickSignOut() {
    $(".signOut").click(function () {
        window.localStorage.clear();
        window.location.href='../../html/login.html';
    })
}

/* 点击企业信息按钮 */
function clickCompanyInfo() {
    $(".companyInfo").click(function () {
        //getAjax("goods");
        window.location.href='../../html/company/companyInfo.html';

    })
}


/* 上传excel文件 */
function sendExcel(element) {

    //var url = "https://dev-app-services.buluo-gs.com/tribalc/v1.0/companies/" + window.localStorage.assign + "/staffs/batch";
    //var url = "https://dev-app-services.buluo-gs.com/tribalc/v1.0/companies/" + window.localStorage.assign + "/staffs/batch";

    var url = Service.URL + "/companies/" + window.localStorage.assign + "/staffs/batch";

    var fileObj = document.getElementById("uploadFile").files[0]; // 获取文件对象
    var FileController = url; // 接收上传文件的后台地址

    if(fileObj){
        // FormData 对象
        var form = new FormData();
        form.append("file", fileObj);// 文件对象
        var token = window.localStorage.token;
        console.log(token);
        // XMLHttpRequest 对象
        var xhr = new XMLHttpRequest();
        xhr.open("post", FileController, true);
        xhr.setRequestHeader('Authorization', token);
        xhr.onload = function () {
            console.log(xhr);
            console.log(xhr.status);
            if (xhr.status == 200) {
                alert("发送成功");
                window.location.href = "../../html/home/staffList.html";

            } else {
                alert("发送失败");
            }
        };
        xhr.send(form);
    }

}

